package com.game.raceroom.loader
{
	import flash.display.*;
	import flash.events.*;
	import flash.net.*;
	import flash.utils.*;

	public class MapLoader
	{
		public var tiles:Array;
		
		public function MapLoader(filename:String)
		{
			var loader:URLLoader = new URLLoader( );
			loader.dataFormat = URLLoaderDataFormat.TEXT;
			loader.addEventListener( Event.COMPLETE, handleComplete );
			loader.load( new URLRequest( filename ) );			
		}
		
		private function handleComplete( event:Event ):void {
			try {
				tiles = new Array;
				// Convert the downlaoded text into an XML instance
				var map:XML = new XML( event.target.data );
				var layerData:XMLList = map.layer.data;
				
				var index:int = 0;
				for each ( var tile:XML in layerData.elements() ) {
				
					index++;
					tiles.push(tile.@["gid"]);
				}
				// At this point, example is ready to be used with E4X
				//trace( example );
			} catch ( e:TypeError ) {
				// If we get here, that means the downloaded text could
				// not be converted into an XML instance, probably because
				// it is not formatted correctly.
				trace( "Could not parse text into XML" );
				trace(e.getStackTrace());
			}
		}
		public function getTiles():Array
		{
			return tiles;
		}		


	}
}